//
//  CerebroSettingVC.swift
//  PlayGround
//
//  Created by admin on 2021/9/17.
//

import UIKit
import SnapKit
class SettingCell: UITableViewCell {
    
    var input:UIView? = nil
    
    override func layoutSubviews() {
        super.layoutSubviews()
        guard let inputs = input else {
            return
        }
        inputs.snp.makeConstraints { snp in
            snp.edges.equalToSuperview().inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    }
}

class CerebroSettingVC: UIViewController {
    
    var scheme:SchemeProtocol?
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
        tableView.register(SettingCell.self, forCellReuseIdentifier: "cell")
        tableView.reloadData()
    }
    
    @IBAction func commitDoit(_ sender: Any) {
        guard let sc = scheme else {
            return
        }
        var param:[String:Any] = [:]
        for item in sc.param{
            for data in item.value {
                param[data.key] = data.inputView.value
            }
        }
        print(param)
    }
    


}
extension CerebroSettingVC:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        return scheme?.param.count ?? 0
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        guard let sc = scheme else {
            return "-"
        }
        let keys = Array(sc.param.keys)
        return keys[section]
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let sc = scheme else {
            return 0
        }
        let keys = Array(sc.param.keys)
        let arr = sc.param[keys[section]]
        return arr?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let sc = scheme else {
            return UITableViewCell()
        }
        let keys = Array(sc.param.keys)
        let arr = sc.param[keys[indexPath.section]]
        
        guard let celldata = arr?[indexPath.row] else {
            return UITableViewCell()
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SettingCell
        cell.input = celldata.inputView
        cell.contentView.addSubview(celldata.inputView)
        return cell
       
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let sc = scheme else {
            return
        }
        let keys = Array(sc.param.keys)
        let arr = sc.param[keys[indexPath.section]]
        
        guard let celldata = arr?[indexPath.row] else {
            return
        }
        if celldata.value.self is PushViewController {
            self.present(celldata.value as! UIViewController, animated: true, completion: nil)
        }
    }
    
    
}
